package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.Specification;
import com.alibaba.shopping_common.pojo.SpecificationOption;
import com.alibaba.shopping_common.pojo.SpecificationOptions;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.SpecificationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SpecificationController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/9 18:16
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;

    @PostMapping("/add")
    public BaseResult<Specification> add(@RequestBody Specification specification){
        specificationService.add(specification);
        return BaseResult.OK(specification);
    }

    @PutMapping("/update")
    public BaseResult<Specification>update(@RequestBody Specification specification){
        specificationService.update(specification);
        return BaseResult.OK(specification);
    }

    /**
      * 删除商品规格
      * @param ids 商品规格id集合     
      * @return 执行结果
      */
    @DeleteMapping("/delete")
    public BaseResult<Long[]>delete(Long[] ids){
        specificationService.delete(ids);
        return BaseResult.OK(ids);
    }

    /**
     * 根据ID查询商品规格
     * @param id 商品规格id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Specification>findById(Long id){
        Specification specification = specificationService.findById(id);
        return BaseResult.OK(specification);
    }

    @GetMapping("/search")
    public BaseResult<Page<Specification>>search(int page,int size){
        Page<Specification> page1 = specificationService.search(page, size);
        return BaseResult.OK(page1);
    }

    /**
     * 查询某种商品类型下的所有规格
     * @param id 商品类型id
     * @return
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId(Long id){
        List<Specification> list = specificationService.findByProductTypeId(id);
        return BaseResult.OK(list);
    }

    /**
     * 新增商品规格项
     * @param options 商品规格项集合
     * @return
     */
    @PostMapping("/addOption")
    public BaseResult<SpecificationOptions> addOption(@RequestBody SpecificationOptions options){
        specificationService.addOption(options);
        return BaseResult.OK(options);
    }

    @DeleteMapping("/deleteOption")
    public BaseResult<Long[]> deleteOption(Long [] ids){
        specificationService.deleteOption(ids);
        return BaseResult.OK(ids);
    }
}




